package cn.heyige.backend.controller;

import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.SensitiveWordDto;
import cn.heyige.backend.service.SensitiveWordService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/sensitiveWords")
@RequiredArgsConstructor
@Tag(name = "敏感词管理", description = "敏感词管理相关接口")
public class SensitiveWordController {
    
    private final SensitiveWordService sensitiveWordService;
    
    /**
     * 检查文本是否包含敏感词
     */
    @PostMapping("check")
    @Operation(summary = "检查文本敏感词", description = "检查文本是否包含敏感词")
    public CommResponse<SensitiveWordDto.CheckResult> checkText(
            @Parameter(description = "待检查的文本", required = true) @RequestBody String text) {
        SensitiveWordDto.CheckResult result = sensitiveWordService.checkText(text);
        return CommResponse.ok(result);
    }
    
    /**
     * 批量检查文本是否包含敏感词
     */
    @PostMapping("checkBatch")
    @Operation(summary = "批量检查文本敏感词", description = "批量检查文本是否包含敏感词")
    public CommResponse<List<SensitiveWordDto.CheckResult>> checkTexts(
            @Parameter(description = "待检查的文本列表", required = true) @RequestBody List<String> texts) {
        List<SensitiveWordDto.CheckResult> results = sensitiveWordService.checkTexts(texts);
        return CommResponse.ok(results);
    }
    
    /**
     * 添加敏感词
     */
    @PostMapping
    @Operation(summary = "添加敏感词", description = "添加新的敏感词")
    public CommResponse<SensitiveWordDto.SensitiveWordResponse> addSensitiveWord(
            @Parameter(description = "添加敏感词请求参数") @Valid @RequestBody SensitiveWordDto.AddRequest request) {
        SensitiveWordDto.SensitiveWordResponse response = sensitiveWordService.addSensitiveWord(request);
        return CommResponse.ok(response);
    }
    
    /**
     * 更新敏感词
     */
    @PutMapping("{id}")
    @Operation(summary = "更新敏感词", description = "更新指定的敏感词")
    public CommResponse<SensitiveWordDto.SensitiveWordResponse> updateSensitiveWord(
            @Parameter(description = "敏感词ID", required = true, example = "1") @PathVariable Long id,
            @Parameter(description = "更新敏感词请求参数") @Valid @RequestBody SensitiveWordDto.UpdateRequest request) {
        SensitiveWordDto.SensitiveWordResponse response = sensitiveWordService.updateSensitiveWord(id, request);
        return CommResponse.ok(response);
    }
    
    /**
     * 删除敏感词
     */
    @DeleteMapping("{id}")
    @Operation(summary = "删除敏感词", description = "删除指定的敏感词")
    public CommResponse<Void> deleteSensitiveWord(
            @Parameter(description = "敏感词ID", required = true, example = "1") @PathVariable Long id) {
        sensitiveWordService.deleteSensitiveWord(id);
        return CommResponse.ok();
    }
    
    /**
     * 获取敏感词列表
     */
    @GetMapping
    @Operation(summary = "获取敏感词列表", description = "分页获取敏感词列表")
    public CommResponse<IPage<SensitiveWordDto.SensitiveWordResponse>> getSensitiveWords(
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<SensitiveWordDto.SensitiveWordResponse> words = sensitiveWordService.getSensitiveWords(page, size);
        return CommResponse.ok(words);
    }
    
    /**
     * 根据分类获取敏感词列表
     */
    @GetMapping("category/{category}")
    @Operation(summary = "根据分类获取敏感词", description = "根据分类分页获取敏感词列表")
    public CommResponse<IPage<SensitiveWordDto.SensitiveWordResponse>> getSensitiveWordsByCategory(
            @Parameter(description = "分类名称", required = true, example = "政治") @PathVariable String category,
            @Parameter(description = "页码", example = "0") @RequestParam(defaultValue = "0") Integer page,
            @Parameter(description = "每页大小", example = "20") @RequestParam(defaultValue = "20") Integer size) {
        IPage<SensitiveWordDto.SensitiveWordResponse> words = sensitiveWordService.getSensitiveWordsByCategory(category, page, size);
        return CommResponse.ok(words);
    }
    
    /**
     * 获取敏感词详情
     */
    @GetMapping("{id}")
    @Operation(summary = "获取敏感词详情", description = "根据ID获取敏感词详情")
    public CommResponse<SensitiveWordDto.SensitiveWordResponse> getSensitiveWord(
            @Parameter(description = "敏感词ID", required = true, example = "1") @PathVariable Long id) {
        SensitiveWordDto.SensitiveWordResponse response = sensitiveWordService.getSensitiveWord(id);
        return CommResponse.ok(response);
    }

    /**
     * 批量导入敏感词
     */
    @PostMapping("import")
    @Operation(summary = "批量导入敏感词", description = "批量导入敏感词")
    public CommResponse<SensitiveWordDto.ImportResult> importSensitiveWords(
            @Parameter(description = "导入敏感词请求参数") @Valid @RequestBody SensitiveWordDto.ImportRequest request) {
        SensitiveWordDto.ImportResult result = sensitiveWordService.importSensitiveWords(request);
        return CommResponse.ok(result);
    }
}